import {getAreaConfig, getIndustryConfig, industryData} from '@/utils/filterConfig';
import {getEsgIndustry, getRiskLevel} from './../apis';
import {ComponentType} from 'vc';
import {useRequest} from 'vue-request';
export function useScreen(marketCode) {
  const PAGE_SIZE = 15;

  const queryData = reactive<any>({
    pageNum: 1,
    pageSize: PAGE_SIZE,
    searchTxt: '',
    sortRule: '',
    provinceCode: '',
    cityCode: '',
    countryCode: '',
    marketCode: marketCode
  });

  // 地区
  const areaData = getAreaConfig();
  areaData.value.submitFn = (val) => {
    const {lv1, lv2, lv3} = val;

    queryData.provinceCode = lv1.join();
    queryData.cityCode = lv2.join();
    queryData.countryCode = lv3.join();
  };


  //ESG行业
  const {data: industryData, run: runEsgIndustry} = useRequest(getEsgIndustry, {
    manual: true,
    defaultParams: [{type: marketCode}]
  });
  const esgIndustryData = {
    title: '行业',
    component: ComponentType.CASCADER,
    options: industryData,
    lv1Single: true,
    submitFn: (data) => {
      const {lv1, lv2, lv3} = data;
      let submitDta = {
        csrcCodeLevel1: "",
        csrcCodeLevel2: "",
        gicsCodeLevel1: "",
        gicsCodeLevel2: "",
        hsCodeLevel1: "",
        hsCodeLevel2: "",
        swCodeLevel1: "",
        swCodeLevel2: "",
      };
      if (lv1[0] === "csrc") {
        submitDta.csrcCodeLevel1 = lv2.join(",");
        submitDta.csrcCodeLevel2 = lv3.join(",");
      } else if (lv1[0] === "gics") {
        submitDta.gicsCodeLevel1 = lv2.join(",");
        submitDta.gicsCodeLevel2 = lv3.join(",");
      } else if (lv1[0] === "hs") {
        submitDta.hsCodeLevel1 = lv2.join(",");
        submitDta.hsCodeLevel2 = lv3.join(",");
      } else if (lv1[0] === "sw") {
        submitDta.swCodeLevel1 = lv2.join(",");
        submitDta.swCodeLevel2 = lv3.join(",");
      }
      const codeObj = {
        'csrc': ['csrcCodeLevel1','csrcCodeLevel2'],
        'gics': ['gicsCodeLevel1','gicsCodeLevel2'],
        'hs': ['hsCodeLevel1','hsCodeLevel2'],
        'sw': ['swCodeLevel1','swCodeLevel2'],
      }
      //过滤三级code，如果三级code全选则不传三级code，只传二级code
      let swCodeLevel2Filter = submitDta[codeObj[lv1[0]][1]].split(",");
      submitDta[codeObj[lv1[0]][0]] &&
      submitDta[codeObj[lv1[0]][0]].split(",").forEach((item) => {
        let newData = industryData.value.filter((fitem) => fitem.code === lv1[0])[0]
          .values;
        newData = newData.filter((fitem2) => fitem2.code === item)[0].values;
        if (
          newData.every(
            (eveItem) => swCodeLevel2Filter.indexOf(eveItem.code) > -1
          )
        ) {
          newData.forEach((rItem) => {
            swCodeLevel2Filter.splice(swCodeLevel2Filter.indexOf(rItem.code), 1);
          });
        }
      });
      Object.keys(submitDta).forEach((item) => {
        delete queryData[item]
      })
      queryData[codeObj[lv1[0]][0]] = submitDta[codeObj[lv1[0]][0]];
      queryData[codeObj[lv1[0]][1]] = swCodeLevel2Filter.join(",");
    }
  };


  // 风险等级
  const {data} = useRequest(getRiskLevel);
  const riskLevelData = {
    title: '风险等级',
    component: ComponentType.MULTIPLE,
    options: data,
    submitFn: (data) => {
      queryData.riskLevel = data;
    }
  };

  // 发生时间
  const dateData = {
    title: '发生时间',
    component: ComponentType.DATE,
    type: 'daterange',
    submitFn: (data) => {
      queryData.eventStartDate = data[0];
      queryData.eventEndDate = data[1];
    }
  };

  const screen = ref([areaData.value, esgIndustryData, riskLevelData, dateData]);

  return {
    PAGE_SIZE,
    screen,
    queryData,
    runEsgIndustry
  };
}
